package cn.fl.capital.service.impl;

import cn.fl.capital.condition.CapFundIntoLogQueryCondition;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.constant.FlowFormUrlConstant;
import cn.fl.capital.constant.FlowInfoConstant;
import cn.fl.capital.dto.CapFundIntoLogDTO;
import cn.fl.capital.transform.CapFundIntoLogVOTransform;
import cn.fl.capital.vo.CapFundIntoLogVO;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.capital.dto.CapFundInOutRDTO;
import cn.fl.capital.condition.CapFundInOutRQueryCondition;
import cn.fl.capital.api.ICapFundInOutRApiService;
import cn.fl.capital.service.ICapFundInOutRService;
import cn.fl.capital.transform.CapFundInOutRVOTransform;
import cn.fl.capital.vo.CapFundInOutRVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 资金池转入转出资金关联表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapFundInOutRServiceImpl implements ICapFundInOutRService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapFundInOutRServiceImpl.class);
	
	/**
	 * 资金池转入转出资金关联表 暴露服务
	 */
	@Autowired
	private ICapFundInOutRApiService capFundInOutRApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private IFlowQueryApiService flowQueryApiService;

	@Override
	public CapFundInOutRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapFundInOutRDTO dto = this.capFundInOutRApiService.selectCapFundInOutRById(id);
		return CapFundInOutRVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapFundInOutRVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capFundInOutRApiService.addCapFundInOutR(CapFundInOutRVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capFundInOutRApiService.removeCapFundInOutRByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapFundInOutRVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capFundInOutRApiService.modifyCapFundInOutR(CapFundInOutRVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapFundInOutRQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapFundInOutRQueryCondition());
        }
		condition=(CapFundInOutRQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capFundInOutRApiService.searchCapFundInOutRListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapFundInOutRVO> voList=CapFundInOutRVOTransform.toVOList((List<CapFundInOutRDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapFundInOutRVO> searchList(CapFundInOutRQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapFundInOutRQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapFundInOutRVO> voList=CapFundInOutRVOTransform.toVOList(this.capFundInOutRApiService.searchCapFundInOutRList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapFundInOutRVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapFundInOutRConstant.CODE_CD);
		for(CapFundInOutRVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public Pager selectTransHistoryList(Pager page) throws BaseException {
		if (null == page) {
			page = new Pager();
			page.setCondition(new CapFundInOutRQueryCondition());
		}
		page = this.capFundInOutRApiService.selectTransRecordListPage(page);
		if (CollectionUtils.isNotEmpty(page.getResultList())) {
			List<CapFundInOutRVO> voList = CapFundInOutRVOTransform.toVOList((List<CapFundInOutRDTO>) page.getResultList());
			if (CollectionUtils.isNotEmpty(voList)) {
				// 查询流程，组织相关的流程数据
				SysFlowSHQueryCondition flowCondition = new SysFlowSHQueryCondition();
				flowCondition.setSysCd(CapitalConstant.SYSCD);
				flowCondition.setWordsInTheTable(FlowInfoConstant.FLOW_LOAN_DY_TABLE);
				List<SysFlowSHDTO> flowList = null;

				for (CapFundInOutRVO vo : voList) {
					flowCondition.setDataId(vo.getOutApplyId());
					flowList = flowQueryApiService.searchFlowSHList(flowCondition);
					if (CollectionUtils.isNotEmpty(flowList)) {
						SysFlowSHDTO sysFlowSHDTO = flowList.get(0);

						vo.setPInstId(sysFlowSHDTO.getPInstId());
						vo.setAudStsCd(sysFlowSHDTO.getAudStsCd());
						vo.setAudStsCdNm(getAudStsCdNm(sysFlowSHDTO.getAudStsCd()));
						vo.setBusinessKey(sysFlowSHDTO.getDataId());
						vo.setPNo(sysFlowSHDTO.getPNo());

					}
				}
				page.setResultList(voList);
			}
		}
		return page;
	}

	private String getAudStsCdNm(String audStsCd) {
		switch (audStsCd) {
			case "0":
				return "待申请";
			case "1":
				return "审核中";
			case "2":
				return "审核通过";
			case "3":
				return "审核不通过";
			case "4":
				return "审核驳回";
			default:
				return "";
		}
	}

}
